package org.potomak.patientmanager.beans.model;

import java.io.Serializable;
import java.util.Set;
import java.util.HashSet;
import java.util.Date;
import java.text.DateFormat;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.jdo.listener.DeleteCallback;


public class PatientBean implements Serializable, DeleteCallback {

	private int id;
	private String name;
	private Date birthDay;
	private String gender;
	private Double height;
        private String notes;
	private Date date;
	private Set checkUps;
	private Set attachments;

	public PatientBean() {
		name = "Test";
		birthDay = new Date();
		gender = "M";
		height = new Double(1.00);
		notes = "notes";
		date = new Date();
		checkUps = new HashSet();
		attachments = new HashSet();
	}

	public PatientBean(String name, Date birthDay, String gender, Double height, String notes, Date date, Set checkUps, Set attachments) {
		this.name = name;
		this.birthDay = birthDay;
		this.gender = gender;
		this.height = height;
		this.notes = notes;
		this.date = date;
		this.checkUps = checkUps;
		this.attachments = attachments;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getBirthDay() {
		return birthDay;
	}

	public void setBirthDay(Date birthDay) {
		this.birthDay = birthDay;
	}
	
	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}
	
	public Double getHeight() {
		return height;
	}

	public void setHeight(Double height) {
		this.height = height;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}
	
	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}
	
	public void addCheckUp(CheckUpBean checkUp) {
		checkUps.add(checkUp);
	}

	public void removeCheckUp(CheckUpBean checkUp) {
		checkUps.remove(checkUp);
	}

	public Set getCheckUps() {
		return checkUps;
	}

	public int getNumberOfCheckUps() {
		return checkUps.size();
	}
	
	public void addAttachment(AttachmentBean attachment) {
		attachments.add(attachment);
	}

	public void removeAttachment(AttachmentBean attachment) {
		attachments.remove(attachment);
	}

	public Set getAttachments() {
		return attachments;
	}

	public int getNumberOfAttachments() {
		return attachments.size();
	}

	public String toString() {
		return "name: " + name + " (" + gender + ") birth: " + DateFormat.getDateInstance().format(birthDay);
	}

    public void jdoPreDelete() {
        System.out.println("jdoPreDelete start");
        PersistenceManager myPM = JDOHelper.getPersistenceManager(this);

        Object[] checkUpsElements = checkUps.toArray();
        Object[] attachmentsElements = attachments.toArray();

        Transaction txn = myPM.currentTransaction();
        try {
            myPM.deletePersistentAll(checkUpsElements);
            myPM.deletePersistentAll(attachmentsElements);

            txn.commit();
        } finally {
            if (txn.isActive()) {
                txn.rollback();
            }
        }
        
        checkUps.clear();
        attachments.clear();

        txn.begin();
        
        System.out.println("jdoPreDelete end");
    }

}